home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 890 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. From: Kresimir Fresl <fresl@master.grad.hr>
  2. Message-ID: <315A8E72.1D3C@master.grad.hr>
  3. X-Original-Date: Thu, 28 Mar 1996 14:04:50 +0100
  4. Path: in1.uu.net!bounce-back
  5. Date: 28 Mar 96 13:22:49 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Organization: Faculty of Civil Engineering, Zagreb, Croatia
  9. X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.01 9000/715)
  10. Cc: Kresimir.Fresl@public.srce.hr
  11. Subject: fstream constructors and strings
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMVqS1eEDnX0m9pzZAQFW5wF/d6F1QtZ+J5PbKO0Y34o46D8jySacR07M
  14.     i1eblh5iWLPvMByaeiXbiJk5DnmP43y3
  15.     =AW+2
  16.  
  17. Why are there no ifstream and ofstream constructors that take a
  18. std::string rather than plain char* for the ``file name''?
  19.  
  20. The following `idiom' was, I think, often used (I use `S', and not `s', 
  21. because this is not a proposed standard string class):
  22.  
  23.      cout >> "Enter file name -> ";
  24.      String file_name;
  25.      ifstream (file_name); // or: ofstream (file_name);
  26.  
  27. Declaring file_name as String, instead of char[file_name_length], 
  28. one does not have to worry about reserving space for file name
  29. (file_name_lengths are very different on different operating systems). 
  30.  
  31. The call to istream/ostream constructor is possible because String 
  32. classes use to have:
  33.     String::operator const char*() const
  34.     
  35. According to proposed standard one should write:
  36.     ifstream (file_name.c_str());
  37. because implicit conversion of string to const char* is not defined.
  38.  
  39. I agree that implicit conversion is (almost always) dangerous, and I
  40. _do not_ propose to add conversion operator to string class. Instead, I
  41. think that fstream classes, beside (or even instead of) constructors that
  42. take const char*s, should have constructors that take strings.
  43.  
  44. In many C++ books we can read that plain C arrays (including char[]s 
  45. and char*s) are to low level, and that we should use vector and string 
  46. classes. Also, it is supposed that iostream classes and operators are 
  47. `on the higher level of abstraction' than cstdio types and functions.
  48. So, why do we have to manualy convert `high level constructs' to `low 
  49. level constructs'?
  50.  
  51.  
  52. Kresimir Fresl
  53. Faculty of Civil Engineering, Zagreb, Croatia
  54. fresl@master.grad.hr
  55. ---
  56. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  57. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  58. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  59. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  60. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  61.